//
//  AppDelegate.swift
//  weibo10
//
//  Created by 晁岳山 on 15/10/14.
//  Copyright © 2015年 晁岳山. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //测试版本
        appearance()
        //加载window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController =  defaultController
        window?.makeKeyAndVisible()
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotifycation, object: nil, queue: nil) { (notify) -> Void in
            let vc = notify.object == nil ? MainViewController() : WelcomeViewController()
            self.window?.rootViewController = vc
        }
        return true
    }
    
    private func appearance() {
        //设置全局外观 一定要在控件创建之前设置
        UINavigationBar.appearance().tintColor = WBAppearrenceTinkColor
        
        UITabBar.appearance().tintColor = UIColor.orangeColor()

    }
}
// MARK: - 多控制器切换
extension AppDelegate {
    //计算性属性
    private var isNewVersion: Bool {
        /**
        
        这儿存在问题  就是第一次从沙盒中去版本的时候  版本是0.0
        
        */
        //1.当前版本
        let currentVersion = Double (NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)
        
        //2.沙盒版本
        let sandBoxVersionKey = "sandBoxVersionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxVersionKey)
        //3.保存至沙盒
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion!, forKey: sandBoxVersionKey)
        
        return currentVersion > sandBoxVersion
    }
    //要显示的控制器
    private var defaultController: UIViewController {
        
        //先判断用户是否登录
        if UserAccountViewModel.sharedUserViewMModel.login {
            return isNewVersion ? NewFeatureViewController() : WelcomeViewController()
        }
        return MainViewController()
    }
    

}
